package com.imooc.service.impl;

import com.imooc.bo.AddressBo;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.UserAddressMapper;
import com.imooc.pojo.UserAddress;
import com.imooc.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.List;

/**
 * <h1>地址</h1>
 *
 * @author : dzj
 * @create: 2022-02-20 16:12
 */
@Slf4j
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private Sid sid;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<UserAddress> queryAll(String userId) {
        UserAddress example = new UserAddress();
        example.setUserId(userId);
        List<UserAddress> addressList = userAddressMapper.select(example);
        return addressList;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void addNewUserAddress(AddressBo addressBo) {
        //1 . 判断当前用户是否存在地址, 如果没有, 则设置为默认地址
        List<UserAddress> userAddresses = queryAll(addressBo.getUserId());
        boolean  isDefaultAddress = CollectionUtils.isEmpty(userAddresses);
        UserAddress ua = addressBo.convert(isDefaultAddress);
        String id = sid.nextShort();
        ua.setId(id);
        userAddressMapper.insert(ua);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateUserAddress(AddressBo addressBo) {
        UserAddress convert = addressBo.convert();
        userAddressMapper.updateByPrimaryKey(convert);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public UserAddress queryUserAddress(String userId, String addressId) {
        UserAddress example = new UserAddress();
        example.setUserId(userId);
        example.setId(addressId);
        UserAddress address = userAddressMapper.selectOne(example);
        return address;
    }

    @Override
    public int deleteUserAddress(String addressId, String userId) {
        return userAddressMapper.deleteByPrimaryKey(addressId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int setDefaultAddress(String addressId, String userId) {
        UserAddress ua = new UserAddress();
        ua.setUserId(userId);
        ua.setIsDefault(YesOrNo.YES.getCode());
        List<UserAddress> select = userAddressMapper.select(ua);
        for (UserAddress userAddress : select) {
            userAddress.setIsDefault(YesOrNo.NO.getCode());
            userAddressMapper.updateByPrimaryKey(userAddress);
        }


        UserAddress userAddress = queryUserAddress(userId, addressId);
        userAddress.setIsDefault(YesOrNo.YES.getCode());
        int i = userAddressMapper.updateByPrimaryKey(userAddress);
        return i;
    }
}
